package com.gugee.ins.common.dal.mapper.product;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.gugee.ins.common.model.mysql.product.InsProduct;
import com.gugee.ins.data.common.msg.InsBloggerMessage;
import com.gugee.ins.data.common.msg.InsProductMessage;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;


/**
 * <p>
 * ins商品表 Mapper 接口
 * </p>
 *
 * @author HuangXin
 * @since 2021-02-25
 */
public interface InsProductMapper extends BaseMapper<InsProduct> {

    /**
     * 查找是否存在，存在则返回
     * @param productIds
     * @return
     */
    @Select("<script>"
            +"select product_id from ins_product where product_id in "
            + "<foreach item='productId' index='index' collection='productIds'      open='(' separator=',' close=')'>"
            + "#{productId}"
            + "</foreach>"
            + "</script>")
    List<Long> findExistUid(@Param("productIds")List<Long> productIds);

    @Select("<script>"
            +"select * from ins_product where product_id in "
            + "<foreach item='productId' index='index' collection='productIds'      open='(' separator=',' close=')'>"
            + "#{productId}"
            + "</foreach>"
            + "</script>")
    List<InsProduct> findByProductIds(@Param("productIds")List<Long> productIds);

    /**
     * 根据productId 查找
     * @param productId
     * @return
     */
    @Select("select * from ins_product where product_id = #{productId} ")
    InsProduct findByProductId(@Param("productId")long productId);

    /**
     * 获取最大id
     * @return
     */
    @Select("select max(id) from ins_product ")
    long getMaxId();

    /**
     * 获取任务列表
     * @param startId
     * @param endId
     * @param limitTime
     * @return
     */
    @Select("select product_id uid,id,overall_category_name user_name " +
            "from ins_product " +
            "where id between #{startId} and #{endId} and last_media_tag_time >= #{limitTime} ")
    List<InsBloggerMessage> findInsProductMessageByPages(long startId, long endId, long limitTime);

    /**
     * 获取分析商品分类任务列表
     * @param startId
     * @param endId
     * @param limitTime
     * @return
     */
    @Select("select id,product_id pid,product_name,description,external_url " +
            " from ins_product " +
            " where id between #{startId} and #{endId} " +
            " and (overall_category_name is null or overall_category_name = '') and last_ai_analysis_category_time < #{limitTime} ")
    List<InsProductMessage> findInsAnalysisProductCategoryMsgByPages(long startId, long endId, long limitTime);
}
